<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/1/5
 * Time: 15:45
 */

namespace app\lib;

use Yii;
class WebService
{

    public static function sendSms($phone, $content, $type = 1)
    {
        if (empty($phone) || empty($content)) {
            return False;
        }
        //   Log::write("进入短信发送:" . $phone . "===" . $content);
//        $location = 'http://sms.mcds.com/services/smsSendService?wsdl';
        $location = 'http://sms.duolahe.com:8080/smsService/services/smsSendService?wsdl';

        //对应WSDL中的targetNamespace
        $uri = 'http://impl.service.mc.numenzq.com/';
//        $namespace = "http://service.mc.numenzq.com/";
        $namespace = "http://webservice.dorabox/";

        $option = array(
            'trace' => 1,
            'location' => $location,
            'uri' => $namespace
        );

        $auth_header = array(
            'key' => 'sms',
            'code' => 'sms',
        );

        $auth_value = new \SoapVar($auth_header, SOAP_ENC_OBJECT, "authentication", $namespace);
        $header = new \SoapHeader($namespace, "authentication", $auth_value);

        $client = new \SoapClient(Null, $option);
        $client->__setSoapHeaders(array($header));

        try {
            //在调用 java axis2 soap 的使用要这么定义, 不然会变成 <param0><item>xxx</item><value>xxx</value></param0>
            //所以会报错. 用一下定义生成的就是这样的格式: <user>asdf</user>
            $p = array(
                new \SoapParam("{'username':'integral', 'password':'integral'}", 'stringJson'),
            );


            $ag_type = 1 == $type ? 21 : 0;  // 短信模板在短信平台定义为21

            $p_sms = array(
                new \SoapParam("{'phoneNumber':'$phone', 'content':[$content, 5],'mark':'888', 'type':'$ag_type'}", 'stringJson'),
            );

            //发送短信
            $result = json_decode($client->__soapCall('sendSms', $p_sms, array('soapaction' => '')), True);

            if ($result['status'] < 0) {
                //throw new Exception('发送短信失败');
                return False;
            }
            return True;
        } catch (\SoapFault $e) {
            // var_dump($e);
            return False;
        } catch (\Exception $e) {
            return False;
        }
    }
        public static function  nc_url(){
                if (YII_ENV=='dev') {
//                    return 'http://10.66.116.246:880/axis/services/OnLineShopService?wsdl';
                    return 'http://10.133.195.207:8880/axis/services/OnLineShopService?wsdl';
//                    return 'http://192.168.54.103:880/axis/services/OnLineShopService?wsdl';
                }else {

                    return 'http://nc.duolahe.com:880/axis/services/OnLineShopService?wsdl';

                }

        }


//    const nc_url='http://192.168.54.28:99/axis/services/OnLineShopService?wsdl';
    //查询库存
    public static function nc_getGiftStock($goodcode) {

        $client = new \SoapClient(static::nc_url(),array('encoding'=>'GBK'));

        $param = array('giftCode'=>$goodcode);
        $re = $client->getGiftStock($param);

       return intval($re->getGiftStockReturn);
    }
    //下订单
    public static function nc_setWxOrder($orderCode, $shrName, $sheng, $shi, $qu, $address, $mobile, $deskphone, $email, $amount
        , $goods_code, $goods_name, $goods_number, $price){
        $client = new \SoapClient(static::nc_url(),array('encoding'=>'GBK'));
        $orderData=[
            "orderCode"=>$orderCode,
            "shrName"=>$shrName,
            "sheng"=>$sheng,
            "shi"=>$shi,
            "qu"=>$qu,
            "address"=>$address,
            "mobile"=>$mobile,
            "deskphone"=>$deskphone,
            "email"=>$email,
            "amount"=>$amount,
            "goods"=>[
                [
                    "goods_code"=>$goods_code,
                    "goods_name"=>$goods_name,
                    "goods_number"=>$goods_number,
                    "price"=>$price,
                ]
             ],

        ];

        $param = array('orderData'=>json_encode($orderData) );
//        $param = array('orderData'=>'{"orderCode":"W14031304000002","shrName":"测试收货人","sheng":"重庆市","shi":"市辖区(重庆市)","qu":"巴南区","address":"渔洞24号中","mobile":"13452467858","deskphone":"0238665485","email":"","amount":"12000.00","goods":[{"goods_code":"DLHCJ0001","goods_name":"麦森艾丽斯汤锅--20cm绿色","goods_number":1,"price":122.67}]}');

         $arr5 = $client->setWxOrder($param);
        $tmp=$arr5->setWxOrderReturn;
        $tmp=iconv('GBK', 'UTF-8', $tmp);

        return json_decode($tmp);
    }

    /**
     * 向业务系统下订单
     * @param $orderData
     * {
     *   'orderCode':'订单编号',
     *   'ordertype':'walfare',
     *   'shrName':'收货人姓名',
     *   'sheng':'省名称',
     *   'shi':'市名称',
     *   'qu':'区名称',
     *   'address':'收货地址',
     *   'mobile':'收货人手机号码',
     *   'deskphone':'',
     *   'email':''',
     *   'amount':订单总价格,
     *   'goods':
     *   [
     *     {
     *       'goods_code':'商品编码',
     *       'goods_name':'商品名称',
     *       'goods_number':商品数量,
     *       'price':商品单价
     *     }
     *   ]
     * }
     * @return mixed
     */
    public static function goods_setWxOrder($orderData)
    {
        $client = new \SoapClient(static::nc_url(), array('encoding' => 'GBK'));
        $param = array('orderData' => json_encode($orderData));
//        $param = array('orderData'=>'{"orderCode":"W14031304000002","shrName":"测试收货人","sheng":"重庆市","shi":"市辖区(重庆市)","qu":"巴南区","address":"渔洞24号中","mobile":"13452467858","deskphone":"0238665485","email":"","amount":"12000.00","goods":[{"goods_code":"DLHCJ0001","goods_name":"麦森艾丽斯汤锅--20cm绿色","goods_number":1,"price":122.67}]}');

        $arr5 = $client->setWxOrder($param);
        $tmp = $arr5->setWxOrderReturn;
        $tmp = iconv('GBK', 'UTF-8', $tmp);

        return json_decode($tmp);
    }
    //查询订单
    public static function nc_getWlxxByWzOrder($orderNo, $giftCode){
        $client = new \SoapClient(static::nc_url(),array('encoding'=>'GBK'));
        $param = array('orderNo'=>$orderNo,'giftCode'=>$giftCode);
        $re = $client->getWlxxByWzOrder($param);

        $tmp=$re->getWlxxByWzOrderReturn;
        $tmp=iconv('GBK', 'UTF-8', $tmp);

        return json_decode($tmp,true);
//        返回值 ：{"status":"1","info":"查询成功","data":{"DLHSM0018":
//{"cpcode":"DLHSM0018","ising":"Y","wlsCode":"","wlsName":"","kdwz":"","wldh":"",
//"sendTime":"","sendStatus":"","ischange":"N","isvirtual":"N","virtualInfo":""},
//"DLHXB0006":{"cpcode":"DLHXB0006","ising":"Y","wlsCode":"","wlsName":"","kdwz":"",
//"wldh":"","sendTime":"","sendStatus":"","ischange":"N","isvirtual":"N","virtualInfo":""}}}

    }
}